package com.app.twth.ui.b2bpick;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.databinding.ItemB2bPickListBinding;
import com.app.twth.interfaces.MyOnClickListener;

import java.util.ArrayList;
import java.util.List;


public class B2BPickAdapter extends RecyclerView.Adapter<B2BPickAdapter.MyViewHolder> {
    private int index = -1;
    private Context mContext;
    private LayoutInflater mInflater;
    private MyOnClickListener myOnClickListener;
    private List<B2BPick> data = new ArrayList<>();
    private MyOnClickListener itemOnClickListener;

    public B2BPickAdapter(Context mContext, List<B2BPick> data, MyOnClickListener myOnClickListener, MyOnClickListener itemOnClickListener) {
        this.mContext = mContext;
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.myOnClickListener = myOnClickListener;
        this.itemOnClickListener = itemOnClickListener;
    }

    public void setData(List<B2BPick> data, int index) {
        this.data = data;
        this.index = index;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemB2bPickListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemB2bPickListBinding itemBinding;

        public MyViewHolder(ItemB2bPickListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            B2BPick pick = data.get(position);
            itemBinding.tvItem.setText("(" + pick.getRefId() + ")" + pick.getPickNo());
            itemBinding.tvInventory.setOnClickListener(v -> itemOnClickListener.OnClick(position));

            itemBinding.tvItem.setOnClickListener(v -> {
                if (myOnClickListener == null) {
                    return;
                }
                myOnClickListener.OnClick(position);
            });
        }
    }

}
